const http = require('../../http/index');
const APICONSTE = require('../../http/apiConst');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    amount: 0,
    accountType: '',
    typeName: '',
    accountName: '',
    creatAt: '',
    id: '',
    type: ''
  },

  setAccountInfo(data) {
    this.setData({
      amount: Number(data.accountVal).toFixed(2),
      accountType: '支出',
      typeName: data.accountTypeName,
      accountName: data.accountName,
      creatAt: data.time
    });
  },

  setIncomeInfo(data) {
    this.setData({
      amount: Number(data.incomeValue).toFixed(2),
      accountType: '收入',
      typeName: data.incomeTypeName,
      accountName: data.incomeMark,
      creatAt: data.incomeTime
    });
  },

  delAccount() {
    var { id, type } = this.data;
    wx.showModal({
      title: '提示',
      content: `您确定要删除吗？`,
      success: async (sm) => {
        if (sm.confirm) {
          var accUrl = APICONSTE.DELETE_ACCOUNT_BY_ID;
          var incUrl = APICONSTE.DELETE_INCOME_BY_ID;
          var url = type === 'pay' ? accUrl : incUrl;
          http({
            action: APICONSTE.DELETE_ACCOUNT_BY_ID,
            data: { id }
          })
            .then(() => {
              wx.showToast({
                icon: 'none',
                title: '删除成功'
              });
              setTimeout(() => {
                wx.navigateBack();
              }, 300);
            })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    var { id, type } = options;
    this.setData({ id, type });
    if (type === 'pay') {
      let { data } = await http({
        action: APICONSTE.QUERY_ACCOUNT_BY_ID,
        data: {
          id: id
        }
      });
      this.setAccountInfo(data);
    } else {
      let { data } = await http({
        action: APICONSTE.QUERY_INCOME_BY_ID,
        data: {
          id: id
        }
      });
      this.setIncomeInfo(data);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})